<?php


namespace app\admin\model;

use think\facade\Cache;
use think\facade\Db;

class Config extends Base
{
    public $routeCacheName = 'adminConfigCache';

    /**
     * Desc: 获取状态码缓存公共方法
     * User: 苏鹏
     * Date: 2023/9/7
     * Time: 16:18
     * @return void
     */
    public function getAdminModelCache(string $type = 'get') : array
    {
        switch ($type){
            case 'get' :
                $cache_res = Cache::get($this->routeCacheName);
                if($cache_res == false){
                    //生成缓存
                    $result = $this->getConfigListToCache();
                    return $result;
                }
                return $cache_res;
            case 'set' :
                $result = $this->getConfigListToCache();
                return $result;
            case 'del' :
                Cache::delete($this->routeCacheName);
                break;
        }
    }

    /**
     * Desc: 获取状态码缓存公共方法 辅助类
     * User: 苏鹏
     * Date: 2023/9/20
     * Time: 10:12
     */
    public function getConfigListToCache()
    {
        $result = Db::name('config')->where('status', 1)
            ->field('flag,content')
            ->select();
        $data = [];
        foreach ($result as $k=>$v)
        {
            $data[$v['flag']] = json_decode($v['content'], true);
        }
        Cache::set($this->routeCacheName, $data);
        return $data;
    }
}